import scrapy
from commonresources.spiders.basespider import BaseSpider
import re
from commonresources.spider_items.shanxi.items import ShanXiShengGongGongZiYuanJiaoYiPingTaiItem
from commonresources.spider_items.base_item import convert_dict
from spidertools.utils.time_utils import get_current_date


class ShanXiShengGongGongZiYuanJiaoYiPingTaiSpider(BaseSpider):
    """"
        山西省公共资源交易平台
    """
    name = 'ShanXiShengGongGongZiYuanJiaoYiPingTai'
    name_zh = "山西省公共资源交易平台"
    province = "山西省"
    start_urls = ["http://prec.sxzwfw.gov.cn/jyxx/index.jhtml"]

    def __init__(self, full_dose=False):
        super(ShanXiShengGongGongZiYuanJiaoYiPingTaiSpider, self).__init__(full_dose)

        self.convert_dict = {"trading_place": "交易场所",
                             "transaction_info_type": "交易信息类型"}
        self.convert_dict.update(convert_dict)

    def parse(self, response):
        divs = response.xpath(".//div[@class='cs_two_nav active_nav']/div")
        inDates = response.xpath(".//input[@id='dates']/@value").extract_first()
        for div in divs:
            project_href = div.xpath("./@onclick").extract_first()
            project_href = re.findall("location.href='(.*?)'", project_href)[0]
            project_type = div.xpath("./text()").extract_first()
            yield scrapy.Request(url=project_href,
                                 callback=self.handle_response,
                                 meta={"project_type": project_type,
                                       "inDates": inDates,
                                       })

    def handle_response(self, response):
        base_url = "http://prec.sxzwfw.gov.cn/queryContent-jyxx.jspx"
        spans = response.xpath(".//div[@id='channel_span']/span")
        for span in spans[1:]:
            val = span.xpath("./@val").extract_first()
            announcement_type = span.xpath("./text()").extract_first()
            yield scrapy.FormRequest(url=base_url,
                                     formdata={"title": "",
                                               "channelId": val,
                                               "origin": "",
                                               "inDates": response.meta["inDates"],
                                               "beginTime": "",
                                               "endTime": ""
                                               },
                                     callback=self.handle2_response,
                                     meta={"announcement_type": announcement_type,
                                           "project_type": response.meta["project_type"],
                                           "need_break": False,
                                           "current_page": "1",
                                           "inDates": response.meta['inDates'],
                                           "channelId": val
                                           })

    def handle2_response(self, response):
        current_page = int(response.meta["current_page"])
        base_url = f"http://prec.sxzwfw.gov.cn/queryContent_{current_page}-jyxx.jspx"
        total_pages = int(re.findall(r"count: (\d+)", response.text)[0])
        a_s = response.xpath(".//div[@class='cs_two_content']/a")
        for a in a_s:
            public_date = a.xpath("./p[@class='cs_bz_cont_1']/span[2]/text()").extract_first().strip().replace("/", "-")
            if not self.full_dose and public_date != get_current_date():
                response.meta["need_break"] = True
            href = a.xpath("./@href").extract_first()
            title = a.xpath("./p[@class='cs_bz_cont']/text()").extract_first().strip()
            trading_place = a.xpath("./p[@class='cs_bz_cont_1']/span[1]/text()").extract_first().strip().split("：")[-1]
            item = {}

            item["release_time"] = public_date
            item["origin_url"] = href
            item["announcement_title"] = title
            item["trading_place"] = trading_place
            item["item"] = ShanXiShengGongGongZiYuanJiaoYiPingTaiItem()
            item["announcement_type"] = response.meta["announcement_type"]
            item["transaction_info_type"] = response.meta["project_type"]
            yield scrapy.Request(url=href,
                                 callback=self.parse_item_new,
                                 meta=item)

        if response.meta["need_break"] is False:
            current_page += 1
            if current_page <= total_pages:
                yield scrapy.FormRequest(url=base_url,
                                         callback=self.handle2_response,
                                         formdata={"title":"",
                                                   "channelId":response.meta['channelId'],
                                                   "origin":"",
                                                   "inDates": response.meta["inDates"],
                                                   "beginTime":"",
                                                   "endTime":""},
                                         meta={"current_page": current_page,
                                               "channelId":response.meta['channelId'],
                                               "inDates": response.meta["inDates"],
                                               "announcement_type": response.meta["announcement_type"],
                                               "project_type":response.meta["project_type"],
                                               "need_break": response.meta["need_break"]}
                                         )